#ifndef __DBSERVERAPP_H__
#define __DBSERVERAPP_H__

#include "../../util/cpp/app/gwapplication.h"
#include "netlib.h"
#include "boost/thread.hpp"
#include "helper/messagecache.h"
#include "IMessageOperator.h"

/**
*@file       dbserverapp.h
*@brief      database application
*
*@author     graywind
*@version    1.0.0.0
*@data       2011/12/27
*/

using namespace gwBedrockApp;

class DBServerApplication : public gwApplication, IMessageOperator
{
  ///@brief    declare application initialize
  GW_DECLARE_APPLICATION;

private:
  ///buffer size
  unsigned int    m_bufferLength;
  ///connection handle
  pnSessionHandle m_connHandle;
  ///mesage cache object
  MessageCache*   m_pMessageCache;

private:
  ///server port
  int            m_Port;
  ///server ip
  std::string    m_strIP;
  ///gateway port
  int            m_GatewayPort;
  ///gateway ip
  std::string    m_strGatewayIP;

private:
  ///@brief    process message
  void ProcessMessage();

public:
  ///@brief    get server ip
  ///retval    std::string&
  const std::string& GetServerIP() const;
  ///@brief    get server port
  ///retval    int
  int                GetServerPort() const;
  ///@brief    get gateway server ip
  ///retval    std::string&
  const std::string& GetGatewayServerIP() const;
  ///@brief    get gateway server port
  ///retval    int
  int                GetGatewayServerPort() const;

public:
  ///@brief    process incoming message
  ///param[in] session_id   identifier of client's connection
  ///param[in] pMsgBuffer   bytes buffer of this message
  ///param[in] msgID        identifier of this message
  ///param[in] msg_bytes    bytes length of message buffer
  ///param[in] type         type of network I/O operation
  ///param[in] error_code   error code of the socket
  ///retval    void
  void OnMessage(int session_id, void* pMsgBuffer, unsigned long msgID, unsigned long msg_bytes, eOperateType type, int error_code);

public:
  ///@brief    send server notify message
  ///param[in] session_id   identifier of the session
  ///retval    void
  void SendNotify(int session_id);
  ///@brief    store message
  ///param[in] session_id   identifier of client's connection
  ///param[in] pMsgBuffer   bytes buffer of this message
  ///param[in] msgID        identifier of this message
  ///param[in] msg_bytes    bytes length of message buffer
  ///retval    void
  void StoreMessage(int session_id, void* pMsgBuffer, unsigned long msgID, unsigned long msg_bytes);
  ///@brief     register to gateway server
  ///param[in]  session_id    identifier of the session
  ///retval     void
  void RegisterServer(int sesion_id);


public:
  ///@brief     constructor
  DBServerApplication();
  ///@brief     destructor
  ~DBServerApplication();

public:
  ///@brief   override open() from gwApplication class
  ///retval   bool
  bool Open();
  ///@brief   override run() from gwApplication class
  ///retval   void
  void Run();
  ///@brief   override close() from gwApplication class
  ///retval   void
  void Close();

};

/***************************************************************************************************************************/
inline
const
std::string&
DBServerApplication::GetServerIP() const
{
	return m_strIP;
}

/***************************************************************************************************************************/
inline
int
DBServerApplication::GetServerPort() const
{
	return m_Port;
}

/***************************************************************************************************************************/
inline
const
std::string&
DBServerApplication::GetGatewayServerIP() const
{
	return m_strGatewayIP;
}

/***************************************************************************************************************************/
inline
int
DBServerApplication::GetGatewayServerPort() const
{
	return m_GatewayPort;
}


#endif
